﻿using System;
using System.Collections.Generic;
using System.Linq;
using System.Text;
using IDAL;
using DataMode;

namespace DAL
{
    class AdminUser:IAdminUser
    {
        DataModeDataContext ctx = new DataModeDataContext();




        public IOrderedQueryable GetOnePageUser(int page)
        {
            return (IOrderedQueryable)((from user in ctx.userinfo
                                        orderby user.username
                                        select user).Skip(6*(page-1)).Take(6));
        }

        public IOrderedQueryable GetOnePageUserByTime(int page)
        {
            return (IOrderedQueryable)((from user in ctx.userinfo
                                        orderby user.register_date
                                        select user).Skip(6 * (page - 1)).Take(6));
        }

        public IOrderedQueryable GetUserByName(string name)
        {
            return (IOrderedQueryable)(from user in ctx.userinfo where user.username.Equals(name) select user);
        }
        public int GetUserCount()
        {
            int userCount = (from user in ctx.userinfo
                                     select user).Count();
            return userCount;
        }
        public IQueryable GetUserInfoByName(string username)
        {
            IQueryable<userinfo> userQuery = from user in ctx.userinfo where user.username.Equals(username) select user;
            return userQuery;
        }

        public void DeleteUserById(int id)
        {
            DataMode.userinfo user = ctx.userinfo.Single( c=> c.userinfo_id == id);
            //IQueryable<video> videoQuery = from v in ctx.video where v.username.Equals(user.username) select v;
            //ctx.video.DeleteAllOnSubmit(videoQuery);
            ctx.userinfo.DeleteOnSubmit(user);
            ctx.SubmitChanges();
        }


        public string GetUserNameByUserID(int userID)
        {
            DataMode.userinfo uif = ctx.userinfo.Single(c => c.userinfo_id == userID);
            if (uif != null)
                return uif.username;
            else
                return null;
        }
    }
}
